#include "ver_comp.h"

/*三个产品，前四个字节为硬件版本号，后四个字节为软件版本号*/
static uint8_t Version_number[4][8] = 
{
    /*0-2硬件版本号 3-6软件版本号*/
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*方向锁硬软件版本号*/
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*读卡器硬软件版本号*/
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*控制器硬软件版本号*/
    {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00} /*字节0为读卡器刷卡次数。字节1为EOl点检标志*/
};

static Ver_canrx_info Ver_rxinfo;
void ver_comp_cancbk(uint8_t len,uint8_t data[])
{
    memcpy(&Ver_rxinfo.Ver_rxinfo.rxdata,data,len);
    
    if ((Ver_rxinfo.Ver_rxinfo.sig.product < VER_MAX) && (Ver_rxinfo.Ver_rxinfo.sig.hd_sw < VER_MX) && (Ver_rxinfo.Ver_rxinfo.sig.reserve == 0XFFFF))
    {
        Ver_rxinfo.rxflag = W_TASK;
    }
}
void ver_comp_init(void)
{
    RTE_FLASH_READ(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
    Ver_rxinfo.rxflag = NO_W_TASK;
    memset(Ver_rxinfo.Ver_rxinfo.rxdata,0,sizeof(Ver_rxinfo.Ver_rxinfo.rxdata));
}

uint8_t Rt_Swipe_Len(void)
{
    uint8_t Swipe_Len = Version_number[3][0];
    return Swipe_Len;
}

bool Rt_eol_flag(void)
{
    bool flag = false;
    if (0x00 != Version_number[3][1])
    {
        flag = true;
    }
    return flag;
}

void Set_eol_flag(bool flag)
{
    Version_number[3][1] = flag;
    RTE_FLASH_ERASE(FLASH_SECTOR_127);
    RTE_FLASH_WRITE(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
}

Rt_result Version_number_comparison(NFCTestStatus Status,Byte0Status St,LCD_TestStatus data_type,uint8_t Version_data[])
{
    Rt_result result = TEST_WAIT;                   /*返回值*/
    int Comparison_results = 0;
    if (STATUS_UNQUALIFIED == St)
    {
        RTE_UART7_LCD_SHOW(Status, data_type, STATUS_UNQUALIFIED); 
        result = TEST_FAIL;
        return result;
    }
    if (HARDWARE_VERSION_NUMBER_CORRECT == data_type)
    {
        Comparison_results = memcmp(&Version_number[Status][0],Version_data,HARD_LEN);
    }else if (SOFTWARE_VERSION_NUMBER_CORRECT == data_type)
    {
        Comparison_results = memcmp(&Version_number[Status][4],Version_data,SOFT_LEN);
    }

    /*比对相同*/
    if (0 == Comparison_results)
    {
        RTE_UART7_LCD_SHOW(Status, data_type, STATUS_QUALIFIED); 
        result = TEST_OK;
    }else{
        RTE_UART7_LCD_SHOW(Status, data_type, STATUS_UNQUALIFIED);
        result = TEST_FAIL;
    }
    return result;
}

void ver_comp_main(void)
{
    int Comparison_results = 0;
    if (W_TASK == Ver_rxinfo.rxflag)
    {
        if (HARDWARE_VER == Ver_rxinfo.Ver_rxinfo.sig.hd_sw)
        {
            memcpy(&Version_number[Ver_rxinfo.Ver_rxinfo.sig.product][0],Ver_rxinfo.Ver_rxinfo.sig.data,HARD_LEN);
            RTE_FLASH_ERASE(FLASH_SECTOR_127);
            RTE_FLASH_WRITE(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
            RTE_FLASH_READ(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
            Comparison_results = memcmp(Ver_rxinfo.Ver_rxinfo.sig.data,&Version_number[Ver_rxinfo.Ver_rxinfo.sig.product][0],HARD_LEN);
        }else if (SOFTWARE_VER == Ver_rxinfo.Ver_rxinfo.sig.hd_sw)
        {
            memcpy(&Version_number[Ver_rxinfo.Ver_rxinfo.sig.product][4],Ver_rxinfo.Ver_rxinfo.sig.data,SOFT_LEN);
            RTE_FLASH_ERASE(FLASH_SECTOR_127);
            RTE_FLASH_WRITE(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
            RTE_FLASH_READ(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
            Comparison_results = memcmp(Ver_rxinfo.Ver_rxinfo.sig.data,&Version_number[Ver_rxinfo.Ver_rxinfo.sig.product][4],SOFT_LEN);
        }else if (NFCRED_VER == Ver_rxinfo.Ver_rxinfo.sig.product && SWIPE_NUM_VER == Ver_rxinfo.Ver_rxinfo.sig.hd_sw)
        {
            memcpy(&Version_number[3][0],Ver_rxinfo.Ver_rxinfo.sig.data,SWIPE_LEN);
            RTE_FLASH_ERASE(FLASH_SECTOR_127);
            RTE_FLASH_WRITE(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
            RTE_FLASH_READ(FLASH_SECTOR_127_ADDR,&Version_number[0][0],sizeof(Version_number));
            Comparison_results = memcmp(Ver_rxinfo.Ver_rxinfo.sig.data,&Version_number[3][0],SWIPE_LEN);
        }
        
        if (STATUS_QUALIFIED == Comparison_results)
        {
            Ver_rxinfo.Ver_rxinfo.sig.reserve = 0x01;
            RTE_CAN_TRANSMIT(VER_COMP_ID,Ver_rxinfo.Ver_rxinfo.rxdata,CAN_LEN);
        }else{
            Ver_rxinfo.Ver_rxinfo.sig.reserve = 0x02;
            RTE_CAN_TRANSMIT(VER_COMP_ID,Ver_rxinfo.Ver_rxinfo.rxdata,CAN_LEN);
        }
        Ver_rxinfo.rxflag = NO_W_TASK;
    }
}
